﻿using Minecraft.CacheRedis;
using Minecraft.Config;
using Minecraft.DALMySql;
using Minecraft.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Minecraft.BLL.mysql
{
	public class GoodsBLL
	{
		private static RedisCacheHelper redisCacheHelper = new RedisCacheHelper();
		private static RedisKey redisKeyEnum = RedisKey.Goods;
		/// <summary>
		/// 向goods表插入数据（分表插入，如果不存在表，则先建立表，并将表名称缓存起来）
		/// </summary>
		/// <param name="model"></param>
		/// <param name="goodsTableNameCacheList"></param>
		/// <returns></returns>
		public static bool InsertSuccessForSplitTable(GoodsModel model, List<string> goodsTableNameCacheList)
		{
			return GoodsDAL.InsertSuccessForSplitTable(model, goodsTableNameCacheList);
		}

		public static GoodsModel GetFirstOrDefault(int playerId, string goodsId)
		{
			return GoodsDAL.GetFirstOrDefault(playerId, goodsId);
		}
		public static List<GoodsModel> GetListAll(int playerId, out bool fromCache)
		{
			fromCache = false;
			//string redisKey = RedisKeyHelper.GetRedisKeyName(redisKeyEnum, playerId.ToString(),
			//	enumGoodsBelongsToType.ToString());
			string redisKey = RedisKeyHelper.GetRedisKeyName(redisKeyEnum, playerId.ToString());
			var list = redisCacheHelper.StringGet<List<GoodsModel>>(redisKey);
			if (list != null)
			{
				fromCache = true;
				return list;
			}
			else
			{
				//list = GoodsDAL.GetListAll(playerId, enumGoodsBelongsToTypeList);
				list = GoodsDAL.GetListAll(playerId);
				redisCacheHelper.StringSet(redisKey, list, CommonConfig.DefRedisExpiry);
				return list;
			}
		}
		/// <summary>
		/// 批量更新goods信息（对于数据库操作时，先删除后插入）
		/// </summary>
		/// <param name="isCacheAfterDbOpt">缓存后是否进行数据库操作</param>
		public static bool BatchUpdateWithTablePrefix(List<GoodsModel> list,
			List<string> goodsTableNameCacheList,
			int playerId, EnumGoodsBelongsToType enumGoodsBelongsToType,
			bool isCacheAfterDbOpt)
		{
			string redisKey = RedisKeyHelper.GetRedisKeyName(redisKeyEnum, playerId.ToString(),
				enumGoodsBelongsToType.ToString());
			redisCacheHelper.StringSet(redisKey, list, CommonConfig.DefRedisExpiry);
			if (isCacheAfterDbOpt)
			{
				return GoodsDAL.BatchUpdateWithTablePrefix(list, goodsTableNameCacheList, playerId, enumGoodsBelongsToType);
			}
			return true;
		}

		///// <summary>
		///// 批量更新goods信息（对于数据库操作时，先删除后插入）
		///// </summary>
		///// <param name="isCacheAfterDbOpt">缓存后是否进行数据库操作</param>
		//public static bool BatchUpdateWithTablePrefix(List<GoodsModel> list,
		//	List<string> goodsTableNameCacheList,
		//	int playerId, List<EnumGoodsBelongsToType> enumGoodsBelongsToTypeList,
		//	bool isCacheAfterDbOpt)
		//{
		//	if (enumGoodsBelongsToTypeList == null || !enumGoodsBelongsToTypeList.Any())
		//	{
		//		throw new Exception("枚举列表不能为空");
		//	}
		//	enumGoodsBelongsToTypeList.ForEach(enumGoodsBelongsToType =>
		//	{
		//		string redisKey = RedisKeyHelper.GetRedisKeyName(redisKeyEnum, playerId.ToString(),
		//		enumGoodsBelongsToType.ToString());
		//		redisCacheHelper.StringSet(redisKey, list.Where(m => m.enumGoodsBelongsToType_Convert == enumGoodsBelongsToType), CommonConfig.DefRedisExpiry);
		//	});
		//	if (isCacheAfterDbOpt)
		//	{
		//		return GoodsDAL.BatchUpdateWithTablePrefix(list, goodsTableNameCacheList, playerId, enumGoodsBelongsToTypeList);
		//	}
		//	return true;
		//}

		/// <summary>
		/// 批量更新goods信息（对于数据库操作时，先删除后插入）
		/// </summary>
		/// <param name="isCacheAfterDbOpt">缓存后是否进行数据库操作</param>
		public static bool BatchUpdateWithTablePrefix(List<GoodsModel> list,
			List<string> goodsTableNameCacheList,
			int playerId,
			bool isCacheAfterDbOpt)
		{
			string redisKey = RedisKeyHelper.GetRedisKeyName(redisKeyEnum, playerId.ToString());
			redisCacheHelper.StringSet(redisKey, list, CommonConfig.DefRedisExpiry);
			if (isCacheAfterDbOpt)
			{
				return GoodsDAL.BatchUpdateWithTablePrefix(list, goodsTableNameCacheList, playerId);
			}
			return true;
		}
	}
}
